package data.utils;

import java.util.Properties;

public class DataProperties {
	public static String MS_SQL_SERVER = "MSSQL";
	public static String MYSQL_SERVER = "MYSQL";
	private String userName;
	private String password;
	private String port;
	private String databaseName;
	private String server;
	private String type;
	private String driver;
	
	public DataProperties(String userName, String password, String port,
			String databaseName, String server, String type, String driver) {
		super();
		this.userName = userName;
		this.password = password;
		this.port = port;
		this.databaseName = databaseName;
		this.server = server;
		this.type = type;
		this.driver = driver;
	}

	public DataProperties(String userName, String password, String port,
			String databaseName, String server, String type) {
		super();
		this.userName = userName;
		this.password = password;
		this.port = port;
		this.databaseName = databaseName;
		this.server = server;
		this.type = type;
	}

	public DataProperties() {
		Properties properties = getDatabasePropertiesFromFile();
		if(properties != null) {
			this.userName = properties.getProperty("database.username");
			this.password = properties.getProperty("database.password");
			this.port = properties.getProperty("database.port");
			this.databaseName = properties.getProperty("database.dbName");
			this.server = properties.getProperty("database.server");
			this.type = properties.getProperty("database.type");
		}
	}

	public String getUserName() {
		return userName;
	}

	public String getPassword() {
		return password;
	}

	public String getPort() {
		return port;
	}

	public String getDatabaseName() {
		return databaseName;
	}

	public String getServer() {
		return server;
	}
	
	public String getDriver() {
		if(this.driver == null || "".equals(this.driver)) {
			if(MS_SQL_SERVER.equalsIgnoreCase(this.type)) {
				this.driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";  
			} else if (MYSQL_SERVER.equalsIgnoreCase(this.type)) {
				this.driver = "com.mysql.jdbc.Driver";
			}
		}
		return driver;
	}
	
	public String getType() {
		return type;
	}
	
	private static Properties getDatabasePropertiesFromFile() {
		Properties properties = new Properties();
		try {
			properties.load(DataProperties.class.getClassLoader().getResourceAsStream("\\resources\\data.properties"));
			return properties;
		} catch (Exception e) {
			System.err.println(e.getMessage().toString());
			return null;
		}
	}
	
	public static void main (String[] args) {
		Properties properties = getDatabasePropertiesFromFile();
		System.out.println(properties.getProperty("database.server"));
	}
}
